package cn.woblog.recyclerviewsample;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by renpingqing on 2017/4/4.
 */

public abstract class BaseRecyclerViewAdapter<D, V extends BaseRecyclerViewAdapter.ViewHolder> extends
    RecyclerView.Adapter<V> {

  private final Context context;
  private final LayoutInflater inflater;
  private List<D> data = new ArrayList<>();
  private OnItemClickListener<V> onItemClickListener;
  private OnItemLongClickListener<V> onItemLongClickListener;

  public BaseRecyclerViewAdapter(Context context) {
    this.context = context;
    inflater = LayoutInflater.from(context);
  }

  @Override
  public void onBindViewHolder(final V holder, final int position) {
    //点击事件
    if (onItemClickListener != null) {
      holder.itemView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          if (onItemClickListener != null) {
            onItemClickListener.onItemClick(holder, position);
          }
        }
      });
    }

    //常按事件
    if (onItemLongClickListener != null) {
      holder.itemView.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
          return onItemLongClickListener.onItemLongClick(holder, position);
        }
      });
    }
  }

  public void addData(List<D> data) {
    this.data.addAll(data);
    notifyItemRangeInserted(data.size() - 1, data.size());
  }

  public void addData(D data) {
    this.data.add(data);
    notifyItemInserted(this.data.size() - 1); //最后一个
  }

  public void addData(int index, D data) {
    this.data.add(index, data);
    notifyItemInserted(index);
  }

  public void removeData(int index) {
    this.data.remove(index);
    notifyItemRemoved(index);
  }

  public void setData(List<D> data) {
    this.data.clear();
    this.data.addAll(data);
    notifyDataSetChanged();
  }

  public D getData(int position) {
    return data.get(position);
  }

  @Override
  public int getItemCount() {
    return data.size();
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  public void setOnItemClickListener(
      OnItemClickListener<V> onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
  }

  public void setOnItemLongClickListener(
      OnItemLongClickListener<V> onItemLongClickListener) {
    this.onItemLongClickListener = onItemLongClickListener;
  }

  public LayoutInflater getInflater() {
    return inflater;
  }

  public Context getContext() {
    return context;
  }

  public interface OnItemClickListener<V> {

    void onItemClick(V holder, int position);
  }


  public interface OnItemLongClickListener<V> {

    boolean onItemLongClick(V holder, int position);
  }

  public abstract class ViewHolder extends RecyclerView.ViewHolder {

    public ViewHolder(View itemView) {
      super(itemView);
      //TODO 可以在这里加入butterknife这样的框架。

    }

    @Nullable
    public final View findViewById(@IdRes int id) {
      return itemView.findViewById(id);
    }

  }


}
